﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;

namespace GmSm.common
{
    
    public static class Util
    {
        //请提供platformId
        public static string platformId = "";
        //测试环境
        public static string Url = "http://hlwyyapi.sdygzx.cn:7077/apitest/supervision";

        #region 通用类
        public static string PostJson(string ServiceId, string ServiceMethod, string body)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
            httpWebRequest.Headers.Add("Encryption-Type", "SM4");
            httpWebRequest.Headers.Add("Platform-Id", platformId);
            httpWebRequest.Headers.Add("Service-Id", ServiceId);
            httpWebRequest.Headers.Add("Service-Method", ServiceMethod);
            httpWebRequest.Headers.Add("Version", "4.3");
            httpWebRequest.Headers.Add("Accept-Charset", "utf-8");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";
            httpWebRequest.Timeout = 30000;
            byte[] btBodys = Encoding.Default.GetBytes(body);
            httpWebRequest.ContentLength = btBodys.Length;
            httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();
            httpWebResponse.Close();
            streamReader.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();
            return responseContent;
        }
        public static string GetBirthDay(string IDCard)
        {
            string birthday = "";
            if (IDCard.Length == 18)
            {
                birthday = IDCard.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            }
            else
            {
                birthday = "19" + IDCard.Substring(6, 6).Insert(4, "-").Insert(2, "-");
            }
            return birthday;
        }

        public static int GetSexCode(string IDCard)
        {
            string Sex = "";
            int SexCode = 1;
            Sex = GetSex(IDCard);
            if (Sex == "男")
            {
                SexCode = 1;

            }
            else
            {
                SexCode = 2;
            }
            return SexCode;
        }

        public static string GetSex(string IDCard)
        {
            string Sex = "";
            if (IDCard.Length == 18)
            {
                int PP = Convert.ToInt32(IDCard.Substring(16, 1)) % 2;
                if (PP == 0)
                {
                    Sex = "女";
                }
                else
                {
                    Sex = "男";
                }

            }
            else
            {
                int PP = Convert.ToInt32(IDCard.Substring(14, 1)) % 2;
                if (PP == 0)
                {
                    Sex = "女";
                }
                else
                {
                    Sex = "男";
                }
            }
            return Sex;
        }
        #endregion
    }
}
